const app = getApp();
Page({
    data: {
        email:'',
        pwd:'',
        api:app.globalData.api
    },
    bindInputEmail(e){
        this.setData({
            email: e.detail.value,
        })
    },
    bindInputPwd(e){
        this.setData({
            pwd: e.detail.value,
        })
    },
    loginSubmit(){
        if(!this.data.email || !this.data.pwd){
            my.showToast({
                type: 'fail',
                content: '邮箱号和密码不为空!',
                duration: 2000
            });
           return false
        }
        let rep = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
        if(!rep.test(this.data.email)){
            my.showToast({
                type: 'fail',
                content: '请输入正确的邮箱格式!',
                duration: 2000
            });
            return false
        }
        console.log(this.data.email);
        console.log(this.data.pwd);
        //调用登录接口
        my.request({
            url: this.data.api+'/login',
            method: 'POST',
            data: {
                email: this.data.email,
                password: this.data.pwd
            },
            headers:{
                'content-type':'application/json'  //默认值
            },
            dataType: 'json',
            success: function(res) {
                if(res.data.errCode==9){
                    my.showToast({
                        type: 'fail',
                        content: res.data.message,
                        duration: 2000
                    });
                    return false;
                }else if(res.data.errCode==0){
                    my.showToast({
                        type: 'success',
                        content: res.data.message,
                        duration: 2000
                    });
                    let resultData = res.data.result;
                    let shareInfo = {};
                    my.getStorage({
                        key: 'shareInfo',
                        success: function(res) {
                            console.log(res.data);
                            shareInfo = res.data;
                            if(!shareInfo){//直接跳到首页
                                my.setStorage({
                                    key: 'userInfo',
                                    data: resultData,
                                    success: function() {
                                        my.reLaunch({
                                            url: '../index/index'
                                        }) 
                                    }
                                });
                                return  
                            }else{//跳到对应的商品详情页
                                my.setStorage({
                                    key: 'userInfo',
                                    data: resultData,
                                    success: function() {
                                        my.reLaunch({
                                            url: '../detail/detail?index='+shareInfo.productId+'&pid='+shareInfo.pid
                                        }) 
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    }
})